package com.ac.scum.log.web.model.player.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.result.Result;
import com.ac.scum.log.bean.Player;
import com.ac.scum.log.web.model.player.service.PlayerConsumeTradeService;
import com.ac.scum.log.web.model.player.service.PlayerService;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.ac.scum.player.bean.PlayerConsumeTrade;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.micrometer.common.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("player_consume")
public class PlayerConsumeController {
    private final PlayerService playerService;
    private final PlayerConsumeTradeService playerConsumeTradeService;

    /**
     * 分页查询消费记录
     */
    @GetMapping("page/{current}/{size}")
    public Result<Object> playerRechargePage(@PathVariable("current") Long current, @PathVariable("size") Long size,
                                             @RequestParam(defaultValue = "", required = false) String playerName,
                                             @RequestParam(defaultValue = "", required = false) String detail,
                                             @RequestParam(defaultValue = "", required = false) Long steamId,
                                             @RequestParam(defaultValue = "", required = false) String adminName,
                                             @RequestParam(defaultValue = "", required = false) Long amount
    ) throws BaseException {
        LambdaQueryWrapper<PlayerConsumeTrade> where = Wrappers.<PlayerConsumeTrade>lambdaQuery().eq(PlayerConsumeTrade::getServerId, CurrentUserUtil.getCurrentServerId());
        if (StringUtils.isNotBlank(playerName)) {
            List<Player> playersByName = playerService.findPlayersByName(CurrentUserUtil.getCurrentServerId(), playerName);
            if (!playersByName.isEmpty()) {
                where.in(PlayerConsumeTrade::getId, playersByName.stream().map(Player::getId).toList());
            }
        }
        if (StringUtils.isNotBlank(detail)) {
            where.like(PlayerConsumeTrade::getConsumeDetail, detail);
        }
        if (steamId != null) {
            where.eq(PlayerConsumeTrade::getPlayerSteamId, steamId);
        }

        if (StringUtils.isNotBlank(adminName)) {
            where.like(PlayerConsumeTrade::getAdminName, adminName);
        }
        if (amount != null) {
            where.gt(PlayerConsumeTrade::getAmount, amount);
        }
        where.orderByDesc(PlayerConsumeTrade::getId);
        Page<PlayerConsumeTrade> page = playerConsumeTradeService.page(new Page<>(current, size), where);
        for (PlayerConsumeTrade record : page.getRecords()) {
            record.setPlayer(playerService.findPlayerById(record.getServerId(), record.getPlayerId()));
        }
        return Result.ok(page);
    }

    /**
     * 查询总消费金额
     */
    @GetMapping("sum_amount")
    public Result<Object> playerRechargeSumAmount(@RequestParam(defaultValue = "", required = false) String playerName,
                                                  @RequestParam(defaultValue = "", required = false) String detail,
                                                  @RequestParam(defaultValue = "", required = false) Long steamId,
                                                  @RequestParam(defaultValue = "", required = false) String adminName,
                                                  @RequestParam(defaultValue = "", required = false) Long amount
    ) throws BaseException {
        StringBuilder sql = new StringBuilder("1=1");
        if (StringUtils.isNotBlank(playerName)) {
            List<Player> playersByName = playerService.findPlayersByName(CurrentUserUtil.getCurrentServerId(), playerName);
            if (!playersByName.isEmpty()) {
                String inSql = playersByName.stream().map(Player::getId).map(String::valueOf).collect(Collectors.joining(","));
                sql.append(" and player_id in (").append(inSql).append(") ");
            }
        }
        if (StringUtils.isNotBlank(detail)) {
            sql.append(" and consume_detail like '%").append(detail).append("%'").append(" ");
        }
        if (steamId != null) {
            sql.append(" and player_steam_id = ").append(steamId).append(" ");
        }
        if (StringUtils.isNotBlank(adminName)) {
            sql.append(" and admin_name like '%").append(adminName).append("%'").append(" ");
        }
        if (amount != null) {
            sql.append(" and amount > ").append(amount).append(" ");
        }
        Long sumAmount = playerConsumeTradeService.getBaseMapper().findSumAmountByWhere(sql.toString());
        if (sumAmount == null) {
            sumAmount = 0L;
        }
        return Result.ok(sumAmount);
    }
}
